import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';
import { deptList } from '#/api/system/dept';
import { getPopupContainer } from '@vben/utils';
// import { getPopupContainer } from '@vben/utils';

export const querySchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    fieldName: 'name',
    label: '项目名称',
  },
  {
    component: 'Input',
    fieldName: 'createByName',
    label: '创建人',
  },
  {
    component: 'RangePicker',
    componentProps: {
      showTime: true,
      format: 'YYYY-MM-DD HH:mm:ss',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
    },
    fieldName: 'createTime',
    label: '创建时间',
  },
  {
    component: 'Select',
    componentProps: {
      getPopupContainer,
      options: [
        { label: '进行中', value: '0' },
        { label: '已结题', value: '1' },
      ],
    },
    fieldName: 'status',
    label: '状态',
  },
];

// 需要使用i18n注意这里要改成getter形式 否则切换语言不会刷新
// export const columns: () => VxeGridProps['columns'] = () => [
export const columns: VxeGridProps['columns'] = [
  { type: 'checkbox', width: 60 },
  {
    title: '项目名称',
    field: 'name',
  },
  {
    title: '创建人',
    field: 'createByName',
    width: 180,
  },
  {
    title: '创建时间',
    field: 'createTime',
  },
  {
    title: '项目描述',
    field: 'description',
  },
  {
    title: '状态',
    field: 'status',
    formatter: ({ cellValue }) => {
      return cellValue === '0' ? '进行中' : cellValue === '1' ? '已结题' : '--';
    },
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 180,
  },
];

export const drawerSchema: FormSchemaGetter = () => [
  {
    label: '',
    fieldName: 'id',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '项目名称',
    fieldName: 'name',
    component: 'Input',
    rules: 'required',
  },
  {
    component: 'ApiSelect',
    componentProps: {
      getPopupContainer,
      // deptCategory: '1' //查机构
      api: () => deptList({ deptCategory: '1' }),
      mode: 'multiple',
      fieldNames: {
        label: 'deptName',
        value: 'deptId',
      },
    },
    fieldName: 'deptIds',
    label: '参与单位',
    rules: 'selectRequired',
  },
  {
    label: '项目描述',
    fieldName: 'description',
    component: 'Textarea',
    rules: 'required',
    formItemClass: 'col-span-2',
  },
  {
    label: '项目是否下设课题',
    fieldName: 'hasTopics',
    component: 'RadioGroup',
    defaultValue: '0',
    componentProps: {
      options: [
        { label: '是', value: '0' },
        { label: '否', value: '1' },
      ],
    },
    rules: 'required',
  },
  {
    label: '课题列表',
    fieldName: 'topicIds',
    component: 'Input', // 只是占位，实际会被插槽替换
    defaultValue: [],
    formItemClass: 'col-span-2',
    dependencies: {
      show: (values) => values.hasTopics === '0',
      triggerFields: ['hasTopics'],
    },
  },
];
